<template>
  <div style="width: 100%; height: 100%">
    <div class="con" v-if="guide">
      <div class="sys_header">
        <div class="logo_header">
          <img v-if="settingFields.logoUrl" :src="uploadURL + PathPrefix + settingFields.logoUrl" class="logo-img"
            alt="" srcset="" />
          <h1>{{ getTruncatedTitle() }}</h1>
        </div>
        <div class="flex justify-between align-center logouts">
          <el-tooltip class="item" :content="fullscreenFlag ? '全屏' : '退出全屏'" effect="light" placement="bottom">
            <span style="color: #38bdea; margin-right: 5px" class="iconfont actinitem"
              :class="[fullscreenFlag ? 'icon-quanping' : 'icon-tuichuquanping']" @click="Fullscreen"></span>
          </el-tooltip>
          <el-dropdown trigger="hover" @command="onCommad" style="margin-right: 20px">
            <span class="el-dropdown-link fontWhiteColor">
              <i class="iconfont icon-guanliyuan" style="color: #38bdea; margin-right: 5px"></i>
              <span style="font-size: 18px">{{ userName }}</span>
              <i class="iconfont icon-xiala tip"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="login" v-if="!IsLogin">
                  <el-icon>
                    <User />
                  </el-icon>
                  登录
                </el-dropdown-item>
                <el-dropdown-item command="logout" v-if="IsLogin">
                  <el-icon>
                    <CircleClose />
                  </el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <div class="sys-body">
        <div class="body-left">
          <div class="card">
            <div class="card-border box" v-for="item in indexButton" :key="item.id" @click="getButtonPath(item)">
              <span class="iconfont" :class="item.icon"> </span>
              <span>{{ item.name }}</span>
            </div>
          </div>
        </div>
        <mjjMove ref="mjjMove" :IsLogin="IsLogin"></mjjMove>
      </div>
      <div class="sys-footer">
        <div class="footer-left">
          <div class="footer-left-all">
            <div class="footer-left-times">{{ times }}</div>
            <div class="footer-left-date">
              <span>{{ date }}</span>
              <span>{{ weekdays }}</span>
            </div>
          </div>
        </div>
        <div class="footer-right">
          <div class="three-card">
            <div class="card-border box" v-for="item in threelist" :key="item.id" @click="getButtonPath(item)">
              <span class="iconfont" :class="item.icon"> </span>
              <span>{{ item.name }}</span>
            </div>
          </div>
        </div>
      </div>
      <loginDialog ref="loginDialog"></loginDialog>
      <versionDialog ref="versionDialog"></versionDialog>
      <span class="version" @click="getVersionDetails()">版本号：V{{ version }}</span>
    </div>
    <stepIndex v-else @getSettingList="getSettingList" @changeGuide="changeGuide"></stepIndex>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
import { sysLogout, settinglist } from '@/api/request'
import { initCheck } from '@/api/init'
import mjjMove from '@/components/mjjMove.vue'
import loginDialog from './loginDialog.vue'
import versionDialog from './versionDialog.vue'
import stepIndex from './stepIndex.vue'
import { ElMessage } from 'element-plus';

export default {
  name: 'SysIndex',
  components: {
    loginDialog,
    mjjMove,
    versionDialog,
    stepIndex,
  },
  data() {
    return {
      guide: true,
      IsLogin: true,
      PathPrefix: 'logo/',
      userName: localStorage.getItem('userName'),
      indexButton: [
        {
          name: '工作主页',
          icon: 'icon-caidan',
          id: '01',
          href: null,
        },
        {
          name: '统计查询',
          icon: 'icon--tongjichaxun',
          id: '09',
          href: null,
        },
        {
          name: '历史记录',
          icon: 'icon-lishijilu1',
          id: '11',
          href: null,
        },
        {
          name: '系统设置',
          icon: 'icon-shezhi',
          id: '10',
          href: null,
        },
        {
          name: '档案录入',
          icon: 'icon-nav_jijiandangan',
          id: '02',
          href: null,
        },
        {
          name: '标签管理',
          icon: 'icon-genghuanmoban',
          id: '03',
          href: null,
        },
        {
          name: '档案上下架',
          icon: 'icon-shangxiajia',
          id: '04',
          href: null,
        },
        {
          name: '出入库管理',
          icon: 'icon-churukuguanli',
          id: '05',
          href: null,
        },
      ],
      isLoggedIn: localStorage.getItem('X-Access-Token'),
      options: [],
      currentTime: new Date(),
      threelist: [
        {
          name: '虚拟库房',
          icon: 'icon-kufangguanli',
          id: '08',
          href: '',
        },
        {
          name: '盘点管理',
          icon: 'icon-navicon-pddsp',
          id: '07',
          href: null,
        },
        {
          name: '更换位置',
          icon: 'icon-icon-exchage',
          id: '06',
          href: null,
        },
      ],
    }
  },
  computed: {
    ...mapState(['fullscreenFlag', 'settingFields', 'AllMenu']),
    times() {
      return this.moment(this.currentTime).format('HH:mm')
    },
    date() {
      return this.moment(this.currentTime).format('YYYY/MM/DD')
    },
    weekdays() {
      return this.moment(this.currentTime).locale('zh-cn').format('dddd')
    },
  },
  watch: {
    userName(newVal, oldVal) {
      if (newVal == '请登录！') {
        this.IsLogin = false
      } else {
        this.IsLogin = true
        this.$refs.mjjMove.getAllCabinet()
      }
      // this.$refs.mjjMove.$emit('update:IsLogin', this.IsLogin)
    },
  },
  created() {
    this.updateTime()
    // 初始化监测  根据监测到数据来掉接口
    this.initCheck()
  },
  mounted() {

    if (!localStorage.getItem('X-Access-Token')) {
      this.userName = '请登录！'
    }
    window.addEventListener('setItemEvent', (e) => {
      //e.key : 是值发生变化的key
      //e.newValue : 是可以对应的新值
      if (e.key === 'X-Access-Token') {
        this.userName = localStorage.getItem('userName')
      }
    })
  },
  methods: {
    ...mapActions(['Fullscreen']),
    changeGuide() {
      this.guide = true
    },
    // 关于验证系统是否被激活 进行的退出登录逻辑
    // 清空缓存
    removeItem() {
      localStorage.removeItem('path')
      localStorage.removeItem('accountId')
      localStorage.removeItem('X-Access-Token')
      this.userName = '请登录！'
    },
    // 初始化 检查ip是否被注册
    async initCheck() {
      /**
       * true   代表已经被注册 不弹出注册界面
       * false  代表没有被注册 弹出注册界面
       */
      const res = await initCheck()
      if (res.data.code == 200) {
        if (res.data.data) {
          this.guide = true
          this.$refs.loginDialog.getPowerMenu()
          this.getSettingList()
          return true
        } else {
          this.guide = false
          this.removeItem()
          return false
        }
      } else {
        this.guide = false
        this.removeItem()
        return false
      }
    },
    // 进行标题截断
    getTruncatedTitle() {
      const title = this.settingFields.sysName ? this.settingFields.sysName : '实体档案管理系统'
      return title.length > 17 ? title.substring(0, 17) + '...' : title
    },
    // 获取版本更新详情
    getVersionDetails() {
      this.$refs.versionDialog.openDialog()
    },
    changeUseName() { },
    // 获取系统信息
    getSettingList() {
      settinglist().then((res) => {
        if (res.data.code == 200) {
          this.ruleForm = res.data.data[0]
          localStorage.setItem('cmPageNo', res.data.data[0].cmPageNo)
          this.$store.dispatch('getSettingFields', res.data.data[0])
        }
      })
    },
    onCommad(command) {
      switch (command) {
        case 'login':
          this.$refs.loginDialog.openDialog()
          // this.onPersonalCenter()
          break
        case 'logout':
          this.onLogout()
          break
      }
    },

    // 退出登录
    onLogout() {
      this.$confirm('是否要退出登录？', '提示', {
        confirmButtonText: '确定',
        confirmButtonClass: 'confirm_btn-class ',
        cancelButtonText: '取消',
      })
        .then(() => {
          sysLogout()
            .then((res) => {
              if (res.data.code == 200) {
                console.log(res);
                
                ElMessage({
                  message: res.data.message,
                  type: 'success',
                })
                this.removeItem()
                this.$router.push('/')
              } else {
                ElMessage.error(res.data.message)
              }
            })
            .catch((err) => { })
        })
        .catch(() => {
          ElMessage({
            type: 'info',
            message: '已取消退出',
          })
        })
    },
    updateTime() {
      setInterval(() => {
        this.currentTime = new Date()
      }, 1000) // 每秒更新一次时间
    },
    getButtonPath(menu) {
      const token = localStorage.getItem('X-Access-Token')
      // 是否有权限可以登录
      const pass = this.AllMenu.some((item) => item.name === menu.name)
      if (token) {
        if (pass) {
          this.$store.commit('getCurrentMenu', menu.id)
          this.$router.push('/home')
        } else {
          ElMessage.error('对不起，您没有当前菜单的权限，请联系管理员！')
        }
      } else {
        // 提示登录；
        this.$refs.loginDialog.openDialog()
      }
    },
  },
  beforeDestroy() {
    if (this.updateTimeInterval) {
      clearInterval(this.updateTimeInterval)
    }
  },
}
</script>

<style lang="less" scoped>
.version {
  position: absolute;
  bottom: 1%;
  right: 3%;
  color: #fff;
  font-size: 14px;
  cursor: pointer;

  &:hover {
    // 鼠标经过有下划线
    text-decoration: underline;
  }
}

.con {
  height: 100%;

  .sys_header {
    position: relative;
    color: #fff;
    height: 15%;
    width: 100%;

    .logo_header {
      position: absolute;
      left: 5%;
      top: 50%;
      transform: translateY(-50%);
      display: flex;
      align-items: center;

      img {
        //width: 1.5625rem /* 300/192 */;
        height: 50px;
        object-fit: contain;
        margin-right: 5px;
      }

      h1 {
        font-family: LiSu !important;
      }
    }

    .logouts {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      right: 50px;
    }
  }

  .sys-body {
    position: relative;

    height: 50%;
    width: 100%;

    .body-left {
      position: absolute;
      left: 5%;
      width: 50%;
      height: 100%;

      .card {
        height: 100%;
        display: flex;
        flex-wrap: wrap;

        .card-border {
          width: calc(25% - 30px);
          height: calc(45% - 35px);
          margin: 15px 10px;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;

          span:nth-child(1) {
            color: #38bdea;
            height: 40px;
            /* 120/192 */
            ;
            font-size: 40px;
            /* 150/192 */
          }

          span:nth-child(2) {
            margin-top: 15px;
            font-size: 18px;
            /* 60/192 */
          }
        }
      }
    }

    .body-right {
      --mjjheight: 60%;
      --selctheight: 15%;

      position: absolute;
      right: 50px;
      width: 40%;
      height: 100%;

      .body-right-box {
        .select-box {
          height: var(--selctheight);
          padding: 10px 0 0 10px;
        }

        .mjj-box {
          display: flex;
          justify-content: center;
          align-items: center;
          height: var(--mjjheight);
          width: 100%;

          .mjj {
            display: flex;
            width: 50px;
            height: 100%;
            margin: 10px 2px;

            justify-content: space-around;
            transform: scale(1);
          }
        }

        .move-button {
          .el-button {
            color: #00bfff;
            background: #2c5ba985;
            border: 1px solid #12408c;
          }
        }
      }
    }
  }

  .sys-footer {
    position: relative;
    height: 35%;
    width: 100%;
    display: flex;

    .footer-left {
      position: absolute;
      left: 5%;
      width: 50%;
      height: 100%;
      display: flex;
      margin-left: 20px;

      .footer-left-all {
        display: flex;
        margin: auto 10px;
        color: #fff;

        .footer-left-times {
          color: #fff;
          font-size: 80px;
          /* 250/192 */
        }

        .footer-left-date {
          display: flex;
          margin: auto 15px;
          flex-direction: column;

          span {
            font-family: 'jiansong' !important;
            font-size: 24px
              /* 70/192 */
            ;
          }
        }
      }
    }

    .footer-right {
      position: absolute;
      right: 50px;
      width: 40%;
      height: 100%;

      .three-card {
        height: 100%;
        display: flex;
        justify-content: center;
        margin: auto;

        .card-border {
          width: calc(31% - 30px);
          height: calc(65% - 35px);
          margin: auto;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;

          span:nth-child(1) {
            color: #38bdea;
            height: 40px
              /* 120/192 */
            ;
            font-size: 40px;
            /* 150/192 */
          }

          span:nth-child(2) {
            margin-top: 15px;
            font-size: 18px;
            /* 60/192 */
          }
        }

        // flex-wrap: wrap;
      }
    }
  }
}

.actinitem {
  cursor: pointer;

  &:hover {
    color: #409eff;
  }
}

.box1 {
  width: 96%;
  height: 96%;
  border: 2px solid rgba(5, 87, 112, 0.6);
}

.box,
.body-right-box {
  position: relative;

  background: rgb(21 103 203 / 20%);
  width: 100%;
  height: 100%;
  line-height: 100%;
  text-align: center;
  color: #dddada;
  text-decoration: none;
  font-size: 20px;
  /* 这里加个动画过渡 */
  transition: all 0.3s ease-in-out;
}

.box::before,
.box::after,
.body-right-box::before,
.body-right-box::after {
  /* 将两个伪元素的相同样式写在一起 */
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  border: 3px solid #38bdea;
  /* 这里也加一个动画过渡 */
  /* 最后的0.3s是延迟时间 */
  transition: all 0.3s ease-in-out 0.3s;
}

.box::before,
.body-right-box::before {
  top: 0;
  left: 0;
  /* 删除左边元素的右、下边框 */
  border-right: 0;
  border-bottom: 0;
  // box-shadow:-4px -4px 10px #f00;
}

.box::after,
.body-right-box::after {
  right: 0;
  bottom: 0;
  /* 删除右边元素的左、上边框 */
  border-top: 0;
  border-left: 0;
}

.box:hover {
  background-color: rgba(56, 189, 234, 0.3);
  // color: #0168b7;
  /* 添加发光效果和倒影 */
  box-shadow: 0 0 50px rgba(56, 189, 234, 0.5);
  /* below是下倒影 1px是倒影和元素相隔的距离 最后是个渐变颜色 */
  /* -webkit-box-reflect: below 1px linear-gradient(transparent,rgba(0,0,0,0.3)); */
  /* 这里我们为以上属性设置一下延迟时间 */
  transition-delay: 0.4s;
}

.box:hover::before,
.box:hover::after {
  width: 98%;
  height: 98%;
  /* 这里不需要延迟 */
  transition-delay: 0s;
}

.select-box {

  // 对element UI的样式进行调整
  .el-input__inner {
    background: transparent;
    border: 1px solid #67707c;
    color: #fff;
  }

  .el-input__inner:focus {
    border: 1px solid #38bdea;
  }

  .el-input__inner::placeholder {
    color: #fff;
  }

  .el-select-dropdown__item.hover,
  .el-select-dropdown__item:hover {
    background-color: #38bdea;
    color: #fff;
  }

  .el-select-dropdown__item {
    color: #dddada;
  }

  .el-button {
    background: transparent;
    color: #fff;
  }

  .el-button:hover {
    background: rgb(21 103 203 / 40%);
    color: #fff;
  }

  .el-select-dropdown {
    border: rgb(32, 56, 101);
    background-color: rgb(32, 56, 101);
  }

  .popper__arrow {
    display: none;
  }

  .el-scrollbar__wrap {
    overflow-x: hidden;
  }
}
</style>
